package com.supermap.dataservice.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**  
 * @Description: 日期操作工具
 * @author zhoujian
 * @date 2018-3-2
 * @version V1.0 
 */
public class DateUtil
{
	private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal();

	  private static final Object object = new Object();
	  
	  /**
	   * 
	   * @Description: 获取对象
	   * @param pattern 日期格式
	   * @return SimpleDateFormat
	   * @throws
	   */
	  private static SimpleDateFormat getDateFormat(String pattern)
	    throws RuntimeException
	  {
	    SimpleDateFormat dateFormat = (SimpleDateFormat)threadLocal.get();
	    if (dateFormat == null) {
	      synchronized (object) {
	        if (dateFormat == null) {
	          dateFormat = new SimpleDateFormat(pattern);
	          dateFormat.setLenient(false);
	          threadLocal.set(dateFormat);
	        }
	      }
	    }
	    dateFormat.applyPattern(pattern);
	    return dateFormat;
	  }
	  /**
	   * 
	   * @Description: 日期上运算
	   * @param date 日期
	   * @param dateType 运算类型 Calendar.HOUR_OF_DAY...
	   * @param amount 运算值
	   * @return Date
	   * @throws
	   */
	private static Date addInteger(Date date, int dateType, int amount)
	  {
	    Date myDate = null;
	    if (date != null) {
	      Calendar calendar = Calendar.getInstance();
	      calendar.setTime(date);
	      calendar.add(dateType, amount);
	      myDate = calendar.getTime();
	    }
	    return myDate;
	  }

	  /**
	   * 
	   * @Description: 日期转换字符串
	   * @param date 日期
	   * @param pattern 目标格式
	   * @return String
	   * @throws
	   */
	  public static String DateToString(Date date, String pattern)
	  {
	    String dateString = null;
	    if (date != null)
	      try {
	        dateString = getDateFormat(pattern).format(date);
	      }
	      catch (Exception localException) {
	      }
	    return dateString;
	  }

	  /**
	   * 
	   * @Description: 字符串转换日期
	   * @param date 字符串格式的日期
	   * @param pattern 目标格式
	   * @return Date
	   * @throws
	   */
	  public static Date StringToDate(String date, String pattern)
	  {
	    Date myDate = null;
	    if (date != null)
	      try {
	        myDate = getDateFormat(pattern).parse(date);
	      }
	      catch (Exception localException) {
	      }
	    return myDate;
	  }
	  
	  /**
	   * 
	   * @Description: 字符串格式的日期转换成另一种字符串格式的日期
	   * @param date 字符串格式日期 
	   * @param olddPattern 原始格式
	   * @param newPattern 目标格式
	   * @return String
	   * @throws
	   */
	  public static String StringToString(String date, String olddPattern, String newPattern)
	  {
	    return DateToString(StringToDate(date, olddPattern), newPattern);
	  }
	  
	  /**
	   * 
	   * @Description: 获取当前时间的毫秒
	   * @return long
	   * @throws
	   */
	  public static long getCurrentTimeMillis()
	  {
		  return System.currentTimeMillis();
	  }

	  /**
	   * 
	   * @Description: 获取当前时间年份
	   * @return int
	   * @throws
	   */
	  public static int getYear(Date date)
	  {
	    return getInteger(date, Calendar.YEAR);
	  }
	  /**
	   * 
	   * @Description: 获取当前时间月份
	   * @return int
	   * @throws
	   */
	  public static int getMonth(Date date)
	  {
	    return getInteger(date, Calendar.MONTH) + 1;
	  }
	  /**
	   * 
	   * @Description: 获取当前时间日
	   * @return int
	   * @throws
	   */
	  public static int getDay(Date date)
	  {
	    return getInteger(date, Calendar.DAY_OF_MONTH);
	  }
	  /**
	   * 
	   * @Description: 获取当前时间小时
	   * @return int
	   * @throws
	   */
	  public static int getHour(Date date)
	  {
	    return getInteger(date, Calendar.HOUR_OF_DAY);
	  }
	  /**
	   * 
	   * @Description: 获取当前时间分钟
	   * @return int
	   * @throws
	   */
	  public static int getMinute(Date date)
	  {
	    return getInteger(date, Calendar.MINUTE);
	  }
	  /**
	   * 
	   * @Description: 获取当前时间秒钟
	   * @return int
	   * @throws
	   */
	  public static int getSecond(Date date)
	  {
	    return getInteger(date, Calendar.SECOND);
	  }
	  private static int getInteger(Date date, int dateType)
	  {
	    int num = 0;
	    Calendar calendar = Calendar.getInstance();
	    if (date != null) {
	      calendar.setTime(date);
	      num = calendar.get(dateType);
	    }
	    return num;
	  }

	  public static int year()
	  {
	  	Calendar calendar = Calendar.getInstance();
	  	calendar.setTime(new Date());
	  	return calendar.get(Calendar.YEAR);
	  }

}
